package com.dbky.alg.core.validate.code;

import com.dbky.alg.core.properties.AlgSecurityProperties;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import java.time.temporal.ChronoUnit;

/**
 * @Auther: tianchao
 * @Date: 2021/12/12 18:37
 * @Description:
 */
@Component("smsValidateCodeGenerator")
public class SmsCodeGenerator implements ValidateCodeGenerator{

    @Autowired
    private AlgSecurityProperties securityProperties;
    @Override
    public ValidateCode generate(ServletWebRequest servletWebRequest) {
        String code = RandomStringUtils.randomNumeric(securityProperties.getCode().getSms().getLength());
        return new ValidateCode(code, securityProperties.getCode().getSms().getExpireIn(), ChronoUnit.SECONDS);
    }
}
